(function () {
    var exports = this;

    var $ = null;
    var dataMonthLength = 6;
    var pageSize = cc.size(600, 370);
    var MonthDayNumArr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    var ClubPowerDate = cc.Layer.extend({

        /**
         *
         * @param year     打开时默认显示的 年
         * @param month    打开时默认显示的 月
         * @param day      打开时默认显示的 日
         * @param callback
         * @param riqifromarr
         * @param riqitoarr
         * @param areaDate  按月计算
         * @returns {boolean}
         */
        ctor: function (year, month, day, callback, riqifromarr, riqitoarr, areaDate) {
            this._super();
            var that = this;
            this._curyear = year;
            this._curmonth = month;
            this._curday = day;
            if (riqifromarr) {
                riqifromarr[0] = parseInt(riqifromarr[0]);
                riqifromarr[1] = parseInt(riqifromarr[1]);
                riqifromarr[2] = parseInt(riqifromarr[2]);
            }
            if (riqitoarr) {
                riqitoarr[0] = parseInt(riqitoarr[0]);
                riqitoarr[1] = parseInt(riqitoarr[1]);
                riqitoarr[2] = parseInt(riqitoarr[2]);
            }
            this._riqifromarr = riqifromarr;
            this._riqitoarr = riqitoarr;
            this._callback = callback;
            this._curPageNum = dataMonthLength - 1;
            this._areaDate = areaDate;

            loadNodeCCS(res.ClubPowerDate_json, this);
            $ = create$(this.getChildByName('Node'));

            $('root').setPosition(cc.p(-300, -300));

            $('root.selectbg').setVisible(false);
            $('root.no1').setVisible(false);

            TouchUtil.setOnclickListener($('bg'), function () {
                that.removeFromParent();
            });
            TouchUtil.setOnclickListener($('root.btnl'), function () {
                var g_idx = parseInt(that._pageView.getCurPageIndex().valueOf());
                if (g_idx > 0) {
                    that._pageView.scrollToPage(g_idx - 1);
                    that.setButtonStatus(g_idx - 1);
                }
            });
            TouchUtil.setOnclickListener($('root.btnr'), function () {
                var g_idx = parseInt(that._pageView.getCurPageIndex().valueOf());
                if (g_idx < dataMonthLength - 1) {
                    that._pageView.scrollToPage(g_idx + 1);
                    that.setButtonStatus(g_idx + 1);
                }
            });

            this.initUI();
            return true;
        },
        initUI: function () {
            var that = this;
            var pageView = new ccui.PageView();
            pageView.setTouchEnabled(true);
            pageView.setContentSize(pageSize);
            pageView.x = 7;
            pageView.y = 0;
            $('root').addChild(pageView);
            this._pageView = pageView;

            var dayJianArr = [];
            var monthArr = [];
            var tmp = 0;
            for (var i = 0; i < dataMonthLength; ++i) {
                var today = new Date();
                var curyear = today.getFullYear();
                var lastmonth = today.getMonth() - i;
                if (lastmonth <= 0) {
                    lastmonth = lastmonth + 12;
                    curyear = curyear - 1;
                }
                var monthDaynum = MonthDayNumArr[lastmonth - 1];
                if (lastmonth == 2 && curyear % 4 == 0) {
                    monthDaynum = 29;
                }
                dayJianArr.push(tmp);
                tmp += monthDaynum;
            }
            // console.log(dayJianArr);
            for (var i = 0; i < dataMonthLength; ++i) {
                var layout = new ccui.Layout();
                layout.setContentSize(pageSize);
                pageView.addPage(layout);


                var today = new Date();
                var curyear = today.getFullYear();
                var curmonth = today.getMonth() + 1 + (i - dataMonthLength + 1);
                if (curmonth <= 0) {
                    curmonth = curmonth + 12;
                    curyear = curyear - 1;
                }
                var curday = today.getDate();
                // console.log(curyear +"-"+ curmonth +"-"+ curday);

                var dayjian = dayJianArr[dataMonthLength - i - 1];
                var curMonthFirstDay = (today.getTime() - (curday - 1 + dayjian) * 86400000);
                // console.log("curMonthFirstDay=="+curMonthFirstDay);
                var curMonthFirstWeek = (new Date(curMonthFirstDay)).getDay();
                // console.log(curyear +"-"+ curmonth +"-星期"+ curMonthFirstWeek+"--"+dayjian);
                monthArr.push(curyear + '-' + curmonth);

                var monthDaynum = MonthDayNumArr[curmonth - 1];
                if (curmonth == 2 && curyear % 4 == 0) {
                    monthDaynum = 29;
                }
                for (var j = 0; j < monthDaynum; j++) {
                    (function (rowi, rowj, curMonthFirstWeek, cury, curm) {
                        var text = null;
                        var indexj = j + (curMonthFirstWeek);
                        var txtpos = cc.p(80 + (indexj % 7 + 0) * 70,
                            330 - Math.floor(indexj / 7) * 60);
                        if (that._curday == (rowj + 1) &&
                            that._curmonth == curm &&
                            that._curyear == cury) {
                            text = new cc.LabelBMFont('' + (rowj + 1), res.powerN1_fnt);
                            var quan = duplicateNode($('root.selectbg'));
                            quan.setVisible(true);
                            quan.setPosition(txtpos);
                            layout.addChild(quan);
                            that._curPageNum = rowi;
                        } else {
                            text = new cc.LabelBMFont('' + (rowj + 1), res.powerN1_fnt);
                        }
                        layout.addChild(text, 2);
                        text.setPosition(txtpos);
                        if (that._areaDate != null) {
                            var result1 = (curm == that._curmonth && that._curday >= rowj);
                            var result2 = (curm == (that._curmonth - that._areaDate) && that._curday < rowj);
                            if (result1 || result2) {
                            } else {
                                Filter.grayMask(text);
                                return;
                            }
                        }
                        TouchUtil.setOnclickListener(text, function () {
                            var today = new Date();
                            if (cury * 365 + curm * 30 + rowj + 1 > today.getFullYear() * 365 + (today.getMonth() + 1) * 30 + today.getDate()) {
                                showAlert('选择的日期不能大于当前日期');
                                return;
                            }
                            //that._riqifromarr
                            if (that._riqifromarr && that._riqifromarr[2] * 365 + that._riqifromarr[1] * 30 + that._riqifromarr[0] > cury * 365 + curm * 30 + rowj + 1) {
                                showAlert('选择的日期不能小于查询起始日期');
                                return;
                            }
                            if (that._riqitoarr && that._riqitoarr[2] * 365 + that._riqitoarr[1] * 30 + that._riqitoarr[0] < cury * 365 + curm * 30 + rowj + 1) {
                                showAlert('选择的日期不能大于查询结束日期');
                                return;
                            }
                            if (that._callback) {
                                that._callback(cury, curm, (rowj + 1))
                            }
                            that.removeFromParent();
                        }, {swallowTouches: true});

                    })(i, j, curMonthFirstWeek, curyear, curmonth);
                }
            }

            pageView.scrollToPage(that._curPageNum);
            $('root.btnl').setVisible(true);
            $('root.btnr').setVisible(false);
            pageView.addEventListener(function (sender, type) {
                switch (type) {
                    case ccui.PageView.EVENT_TURNING:
                        var pageView = sender;
                        var g_idx = parseInt(pageView.getCurPageIndex().valueOf());
                        // console.log(g_idx);
                        $('root.date').setString(monthArr[g_idx]);
                        that.setButtonStatus(g_idx);
                        break;
                    default:
                        break;
                }
            }, that);
        },
        setButtonStatus: function (g_idx) {
            if (g_idx == 0) {
                $('root.btnl').setVisible(false);
                $('root.btnr').setVisible(true);
            } else if (g_idx == (dataMonthLength - 1)) {
                $('root.btnl').setVisible(true);
                $('root.btnr').setVisible(false);
            } else {
                $('root.btnl').setVisible(true);
                $('root.btnr').setVisible(true);
            }
        },
    });

    exports.ClubPowerDate = ClubPowerDate;
})(window);



